#![allow(unused)]

use std::collections::{HashMap, HashSet};

pub struct Solution;

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        let mut res = vec![];
        let mut buf: HashMap<i32, i32> = HashMap::new();
        for (i, n) in nums.iter().enumerate() {
            if buf.get(&(target - n)).is_some() {
                res.push(*buf.get(&(target - n)).unwrap());
                res.push(i as i32);
            } else {
                buf.insert(*n, i as i32);
            }
        }
        res
    }
}
#[test]
fn test_two_sum() {
    assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]);
    assert_eq!(Solution::two_sum(vec![3, 2, 4], 6), vec![1, 2]);
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}

impl ListNode {
    #[inline]
    fn new(val: i32) -> Self {
        ListNode { next: None, val }
    }
}

impl Solution {
    pub fn add_two_numbers(
        l1: Option<Box<ListNode>>,
        l2: Option<Box<ListNode>>,
    ) -> Option<Box<ListNode>> {
        let (mut a, mut b) = (l1, l2);
        let mut head = Box::new(ListNode::new(0));
        let mut curr = &mut head;
        let mut carry = 0;
        while carry > 0 || a.is_some() || b.is_some() {
            let mut sum = carry + a.as_ref().map_or(0, |x| x.val) + b.as_ref().map_or(0, |x| x.val);
            carry = sum / 10;
            curr.next = Some(Box::new(ListNode::new(sum % 10)));
            curr = curr.next.as_mut().unwrap();
            (a, b) = (a.and_then(|x| x.next), b.and_then(|x| x.next));
        }
        head.next
    }
}

fn list_from_vec(v: Vec<i32>) -> Option<Box<ListNode>> {
    let mut head = None;
    for &num in v.iter().rev() {
        let mut node = ListNode::new(num);
        node.next = head;
        head = Some(Box::new(node));
    }
    head
}

#[test]
fn test_add_two_numbers() {
    // 测试用例1: 342 + 465 = 807
    let l1 = list_from_vec(vec![2, 4, 3]);
    let l2 = list_from_vec(vec![5, 6, 4]);
    let result = Solution::add_two_numbers(l1, l2);
    assert_eq!(result, list_from_vec(vec![7, 0, 8]));

    // 测试用例2: 0 + 0 = 0
    let l1 = list_from_vec(vec![0]);
    let l2 = list_from_vec(vec![0]);
    let result = Solution::add_two_numbers(l1, l2);
    assert_eq!(result, list_from_vec(vec![0]));

    // 测试用例3: 9999999 + 9999 = 10009998
    let l1 = list_from_vec(vec![9, 9, 9, 9, 9, 9, 9]);
    let l2 = list_from_vec(vec![9, 9, 9, 9]);
    let result = Solution::add_two_numbers(l1, l2);
    assert_eq!(result, list_from_vec(vec![8, 9, 9, 9, 0, 0, 0, 1]));
}

pub fn length_of_longest_substring(s: String) -> i32 {
    let chars: Vec<char> = s.chars().collect();
    let mut buf: HashSet<char> = HashSet::new();
    let mut l = 0;
    let mut max_len = 0;
    for r in 0..chars.len() {
        while buf.contains(&chars[r]) {
            buf.remove(&chars[l]);
            l += 1;
        }
        buf.insert(chars[r]);
        max_len = max_len.max(r - l + 1);
    }
    max_len as i32
}

#[test]
fn test_length_of_longest_substring() {
    let data = vec![("abcabcbb", 3), ("bbbbb", 1), ("pwwkew", 3)];
    for (d, expect) in data {
        assert_eq!(length_of_longest_substring(d.to_string()), expect);
    }
}
